/**
 * 
 */
package edu.bu.grocery.backingbean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;

import edu.bu.grocery.backingbean.datamodel.GroceryListItemDataModel;
import edu.bu.grocery.session.UserSession;
import edu.bu.grocerylist.model.GroceryList;
import edu.bu.grocerylist.model.GroceryListItem;
import edu.bu.grocerylist.service.IGroceryListService;

/**
 * The bean to help manage the shopping view within the application
 * @author donlanp
 *
 */
public class ShoppingBackingBean implements Serializable {

	private static final long serialVersionUID = -8334740401926111225L;
	
	private UserSession session;
	private IGroceryListService listService;
	
	private GroceryList currentList;
	
	private GroceryListItemDataModel dataModel;
	
	private List<GroceryListItem> selectedItems;
	
	@PostConstruct
	public void init(){
		FacesContext facesContext = FacesContext.getCurrentInstance();
		String listToEdit = facesContext.getExternalContext().getRequestParameterMap().get("groceryList");
		if(listToEdit != null){
			currentList = listService.getGroceryListById(Integer.parseInt(listToEdit));
			
			dataModel = new GroceryListItemDataModel(currentList.getShoppingItems());
			
			//Populate selected items
			selectedItems = new ArrayList<GroceryListItem>();
			for(GroceryListItem item : currentList.getShoppingItems()){
				System.out.println(item.getStatus());
				if(GroceryListItem.SHOPPED.equals(item.getStatus())){
					item.setShopped(true);
					selectedItems.add(item);
				} else {
					item.setShopped(false);
				}
			}
			
			
		}
		
		System.out.println("Init: " + currentList);
		System.out.println("Init: " + selectedItems.size());
	}
	
	/**
	 * Confirms that shopping is finished, saves the newly updated list
	 * @return JSF String for navigating back to the grocery lists page
	 */
	public String finishShopping(){
		System.out.println("Finished: " + currentList);
		System.out.println("Finished: " + selectedItems.size());
		
		for(GroceryListItem item : selectedItems){
			System.out.println("Finished: " + item.getName());
		}
		for(GroceryListItem item : currentList.getShoppingItems()){
			System.out.println("Finished: " + item.getName());
			System.out.println(item.getShopped());
		}
		
		for(GroceryListItem listItem : currentList.getShoppingItems()){
			if(listItem.getShopped()){
				listItem.setStatus(GroceryListItem.SHOPPED);
			} else {
				listItem.setStatus(GroceryListItem.NOT_SHOPPED);
			}
		}
		
		listService.saveGroceryList(currentList);

		return "grocerylists";
	}

	//Getters and Setters
	
	/**
	 * @return the currentList
	 */
	public GroceryList getCurrentList() {
		return currentList;
	}

	/**
	 * @param currentList the currentList to set
	 */
	public void setCurrentList(GroceryList currentList) {
		this.currentList = currentList;
	}

	/**
	 * @return the listService
	 */
	public IGroceryListService getListService() {
		return listService;
	}

	/**
	 * @param listService the listService to set
	 */
	public void setListService(IGroceryListService listService) {
		this.listService = listService;
	}

	/**
	 * @return the session
	 */
	public UserSession getSession() {
		return session;
	}

	/**
	 * @param session the session to set
	 */
	public void setSession(UserSession session) {
		this.session = session;
	}


	/**
	 * @return the dataModel
	 */
	public GroceryListItemDataModel getDataModel() {
		return dataModel;
	}


	/**
	 * @param dataModel the dataModel to set
	 */
	public void setDataModel(GroceryListItemDataModel dataModel) {
		this.dataModel = dataModel;
	}


	/**
	 * @return the selectedItems
	 */
	public List<GroceryListItem> getSelectedItems() {
		return selectedItems;
	}


	/**
	 * @param selectedItems the selectedItems to set
	 */
	public void setSelectedItems(List<GroceryListItem> selectedItems) {
		this.selectedItems = selectedItems;
	}
	
	
	
}
